/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.bookmarksframework.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.core.IFlexDisplayObject;
	import org.bookmarksframework.core.events.FragmentControllerEvent;
	
	
	[Event(name="register", type="org.bookmarksframework.core.events.FragmentControllerEvent")]
	
	[Event(name="unregister", type="org.bookmarksframework.core.events.FragmentControllerEvent")]
	
	[Event(name="registered", type="org.bookmarksframework.core.events.FragmentControllerEvent")]
	
	[Event(name="unregistered", type="org.bookmarksframework.core.events.FragmentControllerEvent")]
	
	[ExcludeClass]
	public class FragmentController extends EventDispatcher implements IFragmentController
	{
		private var _fragment:IFragment;
		
		public function get fragment():IFragment
		{
			return _fragment;
		}
		
		public function set fragment(value:IFragment):void
		{
			_fragment = value;
		}
		
		public function register():void
		{
			if (!fragment.registered)
			{
				var host:IFlexDisplayObject = fragment.host;
				
				host.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
				host.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
				
				if (!host.parent)
				{
					host.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
					return;
				}
				
				host.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
			}
			
			if (host.parent)
				dispatchEvent(new FragmentControllerEvent(FragmentControllerEvent.REGISTERED));
		}
		
		public function unregister():void
		{
			if (fragment.registered)
			{
				var host:IFlexDisplayObject = fragment.host;
				
				// host.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
				host.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
				
				dispatchEvent(new FragmentControllerEvent(FragmentControllerEvent.UNREGISTERED));
			}
		}
		
		public function destroy():void
		{
			var host:IFlexDisplayObject = fragment.host;
			
			host.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			host.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		}
		
		private function addedToStageHandler(event:Event):void
		{
			event.target.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			dispatchEvent(new FragmentControllerEvent(FragmentControllerEvent.REGISTER));
		}
		
		private function removedFromStageHandler(event:Event):void
		{
			event.target.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
			event.target.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			dispatchEvent(new FragmentControllerEvent(FragmentControllerEvent.UNREGISTER));
		}
		
	}
}